package com.newbee.fragement;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.adapter.FriendsListvewAdapter;
import com.newbee.app.OnRefrushActivity;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.listview.FriendsListview;
import com.newbee.listview.onListViewRefreshListener;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;

public class FriendFragment extends Fragment implements OnRefrushActivity,
		onListViewRefreshListener
{
	private FriendsListvewAdapter fla;
	private FriendsListview mListView;
	private TextView emptyTextView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{

		View view = inflater
				.inflate(R.layout.fragment_friend, container, false);
		MainServiceTask mst = new MainServiceTask(TaskType.GET_FRIEND, null,
				FriendFragment.class.getName());
		MainService.addActivityMap(FriendFragment.class.getName(), this);
		MainService.addTaskQueue(mst);
		emptyTextView = (TextView) view.findViewById(R.id.friendlist_empty);
		mListView = (FriendsListview) view.findViewById(R.id.friend_listview);
		mListView.setOnRefreshListener(this);
		return view;
	}

	@Override
	public void onStart()
	{
		// TODO Auto-generated method stub

		super.onStart();
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{
		switch (taskID)
		{
		case TaskType.GET_FRIEND:
		{
			List<FriendsInfoBean> mlist = (List<FriendsInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");
				mListView.setEmptyView(emptyTextView);

			}
			else if (this.fla == null)
			{
				fla = new FriendsListvewAdapter(
						(List<FriendsInfoBean>) paras[0], this.getActivity());
				mListView.setAdapter(fla);
			}
			else
			{
				fla.setList((List<FriendsInfoBean>) paras[0]);
				fla.notifyDataSetChanged();
			}

			break;
		}
		case TaskType.REFRUSH_FRIEND:
		{
			List<FriendsInfoBean> mlist = (List<FriendsInfoBean>) paras[0];
			if (mlist.size() == 0)
			{
				Log.d("wch", "-------------------->");
				mListView.setEmptyView(emptyTextView);

			}
			else if (this.fla == null)
			{
				fla = new FriendsListvewAdapter(
						(List<FriendsInfoBean>) paras[0], this.getActivity());
				mListView.setAdapter(fla);
			}
			else
			{
				fla.setList((List<FriendsInfoBean>) paras[0]);
				fla.notifyDataSetChanged();
			}
			mListView.onRefreshComplete();

			break;
		}
		default:
			break;
		}
		return null;

	}

	@Override
	public void onRefresh()
	{
		MainServiceTask mst = new MainServiceTask(TaskType.REFRUSH_FRIEND,
				null, FriendFragment.class.getName());
		MainService.addActivityMap(FriendFragment.class.getName(), this);
		MainService.addTaskQueue(mst);
	}

	@Override
	public void onDestroy()
	{
		MainService.removeActivity(FriendFragment.class.getName());

		super.onDestroy();
	}

}
